package com.app.util;

import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import lib.common.R;


public abstract class mytoast{
	
	
	public class Bean
	{
		public int animation_id;
		public int show_time;
	}
	
	
	public abstract void OnClick();
	public abstract void OnSetting(Bean bean);
	

	private Handler handler = new Handler();
	private View toast; // 要显示的TipView
	private Activity activity;    
	private Bean bean = new Bean();
	
	public mytoast(Activity activity){
		this.activity = activity;
		
		this.bean.animation_id = R.anim.fadeout;
		this.bean.show_time = 200;
		
		
		this.bean.animation_id = R.anim.fadeout;
		this.bean.show_time = 200;
		
		OnSetting(bean);
		
	}
	
	
/*	public void setting(Bean bean){
		
		if(bean != null){
			this.bean.animation_id = bean.animation_id;
			this.bean.show_time = bean.show_time;
		}
		
	}*/

	public void show(
			ViewGroup layout_content, String msg, boolean bRemoveAllChild) {
		
		if(bRemoveAllChild == true)
			layout_content.removeAllViews();
		else{
			if(layout_content.getChildCount() > 20){  // 如果tips过多将先清空
				layout_content.removeAllViews();
			}
		}
		
		{
			toast = activity.getLayoutInflater().inflate(
					R.layout.layout_title_toast, null);
			TextView tv_title = (TextView)toast.findViewById(R.id.tv_title);
			tv_title.setText(msg);
			tv_title.setOnClickListener( new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					OnClick();
				}
			});
			layout_content.addView(toast);
		}
		toast.setVisibility(View.VISIBLE);
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Animation animation = AnimationUtils.loadAnimation(activity, bean.animation_id);
				animation.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation arg0) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onAnimationRepeat(Animation arg0) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onAnimationEnd(Animation arg0) {
						// TODO Auto-generated method stub
						toast.setVisibility(View.GONE);
					}
				});
				toast.startAnimation(animation);
			}
		}, bean.show_time);
		
	}
	
	
};

/*
public class Utils_Toast {
	private static Utils_Toast mTipView;

	private Utils_Toast() {
	}

	public static Utils_Toast getInstance() {
		if (mTipView == null) {
			mTipView = new Utils_Toast();
		}
		return mTipView;
	}

	private View toast; // 要显示的TipView

	public void show(final Activity activity, Handler handler,
			ViewGroup layout_content, String msg) {
		toast = layout_content.findViewWithTag("layout_title_toast");
		if (null == toast) {
			toast = activity.getLayoutInflater().inflate(
					R.layout.layout_title_toast, null);
			toast.setTag("layout_title_toast");
			layout_content.addView(toast);
		}
		toast.setVisibility(View.VISIBLE);
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Animation animation = AnimationUtils.loadAnimation(activity,
						R.anim.fadeout);
				animation.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation arg0) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onAnimationRepeat(Animation arg0) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onAnimationEnd(Animation arg0) {
						// TODO Auto-generated method stub
						toast.setVisibility(View.GONE);
					}
				});
				toast.startAnimation(animation);
			}
		}, 2000);
	}
}
*/
